import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';
import { getPopupContainer } from '@vben/utils';

import { z } from '#/adapter/form';
import { getDictOptions } from '#/utils/dict';
import { renderTag, renderTags } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'userName',
    label: '登录账号',
    hideLabel: true,
    componentProps: {
      placeholder: '请输入登录账号',
    },
  },
  {
    component: 'Input',
    fieldName: 'nickName',
    label: '用户姓名',
    hideLabel: true,
    componentProps: {
      placeholder: '请输入用户姓名',
    },
  },
  {
    component: 'Input',
    fieldName: 'phonenumber',
    label: '手机号码',
    hideLabel: true,
    componentProps: {
      placeholder: '请输入手机号码',
    },
  },
  {
    component: 'Select',
    fieldName: 'status',
    label: '用户状态',
    hideLabel: true,
    componentProps: {
      optionFilterProp: 'label',
      getPopupContainer,
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
      placeholder: '请选择用户状态',
    },
  },
  // {
  //   component: 'RangePicker',
  //   fieldName: 'createTime',
  //   label: '创建时间',
  //   hideLabel: true,
  //   componentProps: {
  //     placeholder: ['选择创建时间', '选择创建时间'],
  //   },
  // },
];

export const columns: VxeGridProps['columns'] = [
  {
    align: 'center',
    type: 'checkbox',
    width: 45,
    resizable: false,
  },
  {
    field: 'avatar',
    title: '头像',
    slots: { default: 'avatar' },
    align: 'center',
    minWidth: 80,
    width: 80,
  },
  {
    field: 'userName',
    title: '登录账号',
    minWidth: 120,
    width: 120,
    slots: {
      default: ({ row }) => {
        return renderTag(row.userName, '#f50');
      },
    },
  },
  {
    field: 'nickName',
    title: '用户姓名/手机/所属部门',
    minWidth: 400,
    slots: {
      default: ({ row }) => {
        return renderTags(
          [
            // { tagName: row.userName || '无账号', tagColor: 'purple' },
            { tagName: row.nickName || '无昵称', tagColor: 'error' },
            { tagName: row.phonenumber || '暂无手机号', tagColor: '' },
            { tagName: row.deptName || '未分配部门', tagColor: 'processing' },
          ],
          false,
          5,
        );
      },
    },
  },

  // {
  //   field: 'phonenumber',
  //   title: '手机号码',
  //   formatter({ cellValue }) {
  //     return cellValue || '暂无';
  //   },
  //   align: 'center',
  //   width: 120,
  //   minWidth: 120,
  // },
  // {
  //   field: 'deptName',
  //   title: '所属部门',
  //   minWidth: 140,
  // },
  // {
  //   field: 'createTime',
  //   title: '创建时间',
  //   align: 'center',
  //   minWidth: 160,
  //   width: 160,
  // },
  {
    field: 'status',
    title: '状态',
    slots: { default: 'status' },
    align: 'center',
    minWidth: 100,
    width: 100,
  },
  {
    field: 'action',
    fixed: 'right',
    align: 'center',
    slots: { default: 'action' },
    title: '操作',
    resizable: false,
    width: 'auto',
  },
];

export const drawerSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'userId',
  },
  {
    component: 'Divider',
    componentProps: {
      orientation: 'center',
    },
    fieldName: 'divider1',
    hideLabel: true,
    renderComponentContent: () => ({
      default: () => '账号信息',
    }),
  },
  {
    component: 'Input',
    fieldName: 'userName',
    label: '登录账号',
    rules: 'required',
    componentProps: {
      placeholder: '请输入登录账号',
    },
  },
  {
    component: 'InputPassword',
    fieldName: 'password',
    label: '登录密码',
    rules: 'required',
    componentProps: {
      placeholder: '请输入登录密码',
    },
  },
  {
    component: 'Select',
    defaultValue: '0',
    fieldName: 'status',
    label: '账号状态',
    rules: 'required',
    componentProps: {
      optionFilterProp: 'label',
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
    },
  },
  {
    component: 'Divider',
    componentProps: {
      orientation: 'center',
    },
    fieldName: 'divider1',
    hideLabel: true,
    renderComponentContent: () => ({
      default: () => '基本信息',
    }),
  },
  {
    component: 'Input',
    fieldName: 'nickName',
    label: '用户姓名',
    rules: 'required',
    componentProps: {
      placeholder: '请输入用户姓名',
    },
    formItemClass: 'col-span-2 lg:col-span-1',
  },
  {
    component: 'Select',
    fieldName: 'sex',
    label: '用户性别',
    rules: 'required',
    componentProps: {
      optionFilterProp: 'label',
      options: getDictOptions(DictEnum.SYS_USER_SEX),
      optionType: 'button',
    },
    defaultValue: '0',
    formItemClass: 'col-span-2 lg:col-span-1',
  },
  {
    component: 'Input',
    fieldName: 'phonenumber',
    label: '手机号码',
    defaultValue: undefined,
    componentProps: {
      placeholder: '请输入手机号码',
    },
    rules: z
      .string()
      .regex(/^1[3-9]\d{9}$/, '请输入正确的手机号码')
      .optional()
      .or(z.literal('')),
    formItemClass: 'col-span-2 lg:col-span-1',
  },
  {
    component: 'Input',
    fieldName: 'email',
    defaultValue: undefined,
    componentProps: {
      placeholder: '请输入电子邮箱',
    },
    label: '电子邮箱',
    /**
     * z.literal 是 Zod 中的一种类型，用于定义一个特定的字面量值。
     * 它可以用于确保输入的值与指定的字面量完全匹配。
     * 例如，你可以使用 z.literal 来确保某个字段的值只能是特定的字符串、数字、布尔值等。
     * 即空字符串也可通过校验
     */
    rules: z
      .string()
      .email('请输入正确的电子邮箱')
      .optional()
      .or(z.literal('')),
    formItemClass: 'col-span-2 lg:col-span-1',
  },
  {
    component: 'Divider',
    componentProps: {
      orientation: 'center',
    },
    fieldName: 'divider1',
    hideLabel: true,
    renderComponentContent: () => ({
      default: () => '角色权限',
    }),
  },
  {
    component: 'TreeSelect',
    // 在drawer里更新 这里不需要默认的componentProps
    defaultValue: undefined,
    fieldName: 'deptId',
    label: '所属部门',
    rules: 'selectRequired',
  },
  {
    component: 'Select',
    componentProps: {
      optionFilterProp: 'label',
      getPopupContainer,
      mode: 'multiple',
      optionLabelProp: 'label',
      placeholder: '选择所属部门后, 将自动加载该部门下的岗位',
    },
    fieldName: 'postIds',
    // help: '选择所属部门后, 将自动加载该所属所属部门下所有的岗位',
    label: '部门岗位',
  },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      mode: 'multiple',
      optionFilterProp: 'title',
      optionLabelProp: 'title',
      placeholder: '请选择角色',
    },
    fieldName: 'roleIds',
    label: '所属角色',
  },
  {
    component: 'Textarea',
    fieldName: 'remark',
    formItemClass: 'items-start',
    label: '备注描述',
    componentProps: {
      placeholder: '请输入备注描述',
      autosize: true,
    },
  },
];
